package com.neusoft.controller;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.entity.User;
import com.neusoft.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	UserService userService;

	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request, String account, String password) {
		ModelAndView modelAndView = new ModelAndView();
		User user = userService.loadListByExample(account, password).get(0); // 可以get（0）吗？？？？？
		if (user != null) {
			modelAndView.addObject("loginMessage", "登录成功！");
			modelAndView.addObject("user", user);
			// 指定视图
			modelAndView.setViewName("user/books");
			request.getSession().setAttribute("user", user);
		} else {
			modelAndView.addObject("loginMessage", "登录失败！");
			modelAndView.setViewName("user/login");
		}
		return modelAndView;
	}

	@RequestMapping("/register")
	public ModelAndView register(HttpServletRequest request, String username, String password, String confirm,
			String sex, String email, String phone) {
		ModelAndView modelAndView = new ModelAndView();
		if (username == null || password == null || confirm == null || username.trim().isEmpty()
				|| password.trim().isEmpty() || confirm.trim().isEmpty()) {
			modelAndView.addObject("errorMess", "用户名或密码不能为空，注册失败!");
			modelAndView.setViewName("user/regist");
		} else if (!password.equals(confirm)) {
			modelAndView.addObject("errorMess", "两次输入的密码不一致，注册失败！");
			modelAndView.setViewName("user/regist");
		} else {
			modelAndView.addObject("successMess", "注册成功！");
			modelAndView.setViewName("user/login");
			// 向数据库增加用户信息
			User user = new User();
			user.setuName(username);
			user.setuPassword(password);
			int sexNum = 1;
			if (sex.equals("女")) {
				sexNum = 0;
			}
			user.setuSex(sexNum);
			user.setuEmail(email);
			user.setuPhone(phone);
			userService.addSelective(user);
			request.getSession().setAttribute("user", user);
		}
		return modelAndView;
	}

	/////////////////////////////////////////////////////
	@RequestMapping("/selectByUserID")
	public ModelAndView selectByUserID(Integer ID) {
		ModelAndView modelAndView = new ModelAndView("user/userinformation");
		User user = userService.loadByUserId(ID);
		modelAndView.addObject("user", user);
		return modelAndView;
	}

	//////////////////// ???????????????????????????????????干什么的？
	@RequestMapping("/getInformation")
	public ModelAndView getInformation(Integer ID) {
		ModelAndView modelAndView = new ModelAndView("user/");
		User user = userService.loadByUserId(ID);
		modelAndView.addObject("user", user);
		return modelAndView;
	}

	@RequestMapping("/modifyPassword")
	public ModelAndView modifyPassword(HttpServletRequest request, String oldPassword, String newPassword,
			String confirm) {
		ModelAndView modelAndView = new ModelAndView("user/userinformation");
		if (oldPassword == null || newPassword == null || confirm == null || oldPassword.trim().isEmpty()
				|| newPassword.trim().isEmpty() || confirm.trim().isEmpty()) {
			modelAndView.addObject("errorMess", "密码不能为空，修改密码失败!");
		} else if (!newPassword.equals(confirm)) {
			modelAndView.addObject("errorMess", "新密码和确认密码不一致，修改密码失败！");
		} else {
			modelAndView.addObject("successMess", "修改成功！");
			User user = (User) request.getSession().getAttribute("user");
			user.setuPassword(newPassword);
			userService.updateByUserIdSelective(user);
			request.getSession().setAttribute("user", user); // 同个user吗???????????????
		}
		return modelAndView;
	}

	// 更新个人信息
	@RequestMapping("/updateUser")
	public ModelAndView updateOperator(User user) {
		ModelAndView modelAndView = new ModelAndView("user/userinformation");
		userService.updateByUserIdSelective(user);
		modelAndView.addObject("user", user);
		return modelAndView;
	}

	@RequestMapping("/modifyBalance")
	public ModelAndView modifyBalance(HttpServletRequest request, BigDecimal num) {
		ModelAndView modelAndView = new ModelAndView("???????????");
		User user = (User) request.getSession().getAttribute("user");
		BigDecimal oldBalance = user.getBalance();
		user.setBalance(oldBalance.add(num));
		userService.updateByUserIdSelective(user);
		modelAndView.addObject("user", user);
		return modelAndView;
	}
}
